package ve.edu.ucla.dals.servicio.ui;

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JSeparator;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.eclipse.jetty.server.Server;

import ve.edu.ucla.dals.modelo.ConexionSQL;
import ve.edu.ucla.dals.servicio.ManejadorDespacho;
import ve.edu.ucla.dals.servicio.server.Loader;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainDialog extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private final JPanel contentPanel = new JPanel();
	private JTextField txtUsuario;
	private JTextField txtClave;
	private JTextField txtDireccion;
	private JTextField txtPuerto;
	private Server servidor = null;
	private boolean iniciado = false;

	/**
	 * Create the dialog.
	 */
	public MainDialog() {
		setResizable(false);
		setTitle("DALS API");
		//setName("DALS API");
		setBounds(100, 100, 453, 275);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		SpringLayout sl_contentPanel = new SpringLayout();
		contentPanel.setLayout(sl_contentPanel);
		
		final JButton btnIniciarServidor = new JButton("Iniciar servidor");
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, btnIniciarServidor, -10, SpringLayout.SOUTH, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.EAST, btnIniciarServidor, -10, SpringLayout.EAST, contentPanel);
		btnIniciarServidor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (iniciado) {
						servidor.stop();
						iniciado = false;
						txtUsuario.setEnabled(true);
						txtClave.setEnabled(true);
						txtDireccion.setEnabled(true);
						txtPuerto.setEnabled(true);
						btnIniciarServidor.setText("Iniciar servidor");
						servidor = null;
					} else {
						ConexionSQL conexion = new ConexionSQL(
							/* usuario = */ txtUsuario.getText(),
							/* clave = */   txtClave.getText(),
							/* urlBD = */   txtDireccion.getText());
					
						servidor = Loader.start(new ManejadorDespacho(conexion), Integer.parseInt(txtPuerto.getText()));
						iniciado = true;
						txtUsuario.setEnabled(false);
						txtClave.setEnabled(false);
						txtDireccion.setEnabled(false);
						txtPuerto.setEnabled(false);
						btnIniciarServidor.setText("Detener servidor");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		contentPanel.add(btnIniciarServidor);
		
		JLabel lblBaseDeDatos = new JLabel("Base de datos:");
		lblBaseDeDatos.setFont(new Font("Tahoma", Font.BOLD, 11));
		sl_contentPanel.putConstraint(SpringLayout.NORTH, lblBaseDeDatos, 10, SpringLayout.NORTH, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblBaseDeDatos, 10, SpringLayout.WEST, contentPanel);
		contentPanel.add(lblBaseDeDatos);
		
		JLabel lblUsuario = new JLabel("Usuario:");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, lblUsuario, 9, SpringLayout.SOUTH, lblBaseDeDatos);
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblUsuario, 0, SpringLayout.WEST, lblBaseDeDatos);
		contentPanel.add(lblUsuario);
		
		txtUsuario = new JTextField("root");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, txtUsuario, -3, SpringLayout.NORTH, lblUsuario);
		sl_contentPanel.putConstraint(SpringLayout.WEST, txtUsuario, 60, SpringLayout.EAST, lblUsuario);
		sl_contentPanel.putConstraint(SpringLayout.EAST, txtUsuario, -136, SpringLayout.EAST, contentPanel);
		contentPanel.add(txtUsuario);
		txtUsuario.setColumns(10);
		
		txtClave = new JTextField("");
		sl_contentPanel.putConstraint(SpringLayout.EAST, txtClave, -136, SpringLayout.EAST, contentPanel);
		contentPanel.add(txtClave);
		
		JLabel lblContrasea = new JLabel("Contrase\u00F1a:");
		sl_contentPanel.putConstraint(SpringLayout.WEST, txtClave, 40, SpringLayout.EAST, lblContrasea);
		sl_contentPanel.putConstraint(SpringLayout.NORTH, lblContrasea, 15, SpringLayout.SOUTH, lblUsuario);
		sl_contentPanel.putConstraint(SpringLayout.NORTH, txtClave, -3, SpringLayout.NORTH, lblContrasea);
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblContrasea, 0, SpringLayout.WEST, lblBaseDeDatos);
		contentPanel.add(lblContrasea);
		
		txtDireccion = new JTextField("jdbc:mysql://localhost:3306/dals_v3");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, txtDireccion, 6, SpringLayout.SOUTH, txtClave);
		sl_contentPanel.putConstraint(SpringLayout.WEST, txtDireccion, 0, SpringLayout.WEST, txtUsuario);
		sl_contentPanel.putConstraint(SpringLayout.EAST, txtDireccion, -10, SpringLayout.EAST, contentPanel);
		contentPanel.add(txtDireccion);
		txtDireccion.setColumns(10);
		
		JLabel lblHost = new JLabel("Host:");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, lblHost, 3, SpringLayout.NORTH, txtDireccion);
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblHost, 0, SpringLayout.WEST, lblBaseDeDatos);
		contentPanel.add(lblHost);
		
		JSeparator separator = new JSeparator();
		sl_contentPanel.putConstraint(SpringLayout.NORTH, separator, 6, SpringLayout.SOUTH, txtDireccion);
		sl_contentPanel.putConstraint(SpringLayout.WEST, separator, 0, SpringLayout.WEST, lblBaseDeDatos);
		sl_contentPanel.putConstraint(SpringLayout.EAST, separator, 0, SpringLayout.EAST, btnIniciarServidor);
		contentPanel.add(separator);
		
		JLabel lblServidor = new JLabel("Servidor:");
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, separator, -6, SpringLayout.NORTH, lblServidor);
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblServidor, 0, SpringLayout.WEST, lblBaseDeDatos);
		lblServidor.setFont(new Font("Tahoma", Font.BOLD, 11));
		contentPanel.add(lblServidor);
		
		txtPuerto = new JTextField("3257");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, txtPuerto, 42, SpringLayout.SOUTH, txtDireccion);
		sl_contentPanel.putConstraint(SpringLayout.WEST, txtPuerto, 49, SpringLayout.EAST, lblServidor);
		sl_contentPanel.putConstraint(SpringLayout.EAST, txtPuerto, -224, SpringLayout.EAST, contentPanel);
		contentPanel.add(txtPuerto);
		txtPuerto.setColumns(10);
		
		JLabel lblPuerto = new JLabel("Puerto");
		sl_contentPanel.putConstraint(SpringLayout.NORTH, lblPuerto, 155, SpringLayout.NORTH, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, lblServidor, -6, SpringLayout.NORTH, lblPuerto);
		sl_contentPanel.putConstraint(SpringLayout.WEST, lblPuerto, 0, SpringLayout.WEST, lblBaseDeDatos);
		contentPanel.add(lblPuerto);
		
		JSeparator separator_1 = new JSeparator();
		sl_contentPanel.putConstraint(SpringLayout.NORTH, separator_1, 10, SpringLayout.SOUTH, txtPuerto);
		sl_contentPanel.putConstraint(SpringLayout.WEST, separator_1, 10, SpringLayout.WEST, contentPanel);
		sl_contentPanel.putConstraint(SpringLayout.SOUTH, separator_1, -6, SpringLayout.NORTH, btnIniciarServidor);
		sl_contentPanel.putConstraint(SpringLayout.EAST, separator_1, 0, SpringLayout.EAST, btnIniciarServidor);
		contentPanel.add(separator_1);
	}
}
